package com.mask.mq.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

/**
 * 发送延迟消息
 */

@Controller
@Slf4j
@RequestMapping("/ttl")
public class SendMessageController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //发消息
    @GetMapping("/sendMsg/{message}")
    public void sendMsg(@PathVariable String message){
        log.info("当前时间:{},发送一条信息给两个TTL队列:{}",new Date().toString(),message);

        rabbitTemplate.convertAndSend("X","XA","消息来自TTL为10s的队列"+message);
        rabbitTemplate.convertAndSend("X","XB","消息来自TTL为40s的队列"+message);
    }

    @GetMapping("/sendExpirationMsg/{message}/{ttlTime}")
    public void sendMsg(@PathVariable String message,@PathVariable String ttlTime){
        log.info("当前时间:[{}],发送一条时长[{}]毫秒TTL信息给队列QC:[{}]",new Date().toString(),ttlTime,message);
        rabbitTemplate.convertAndSend("X","XC",message,message1 -> {
            //设置发送消息时候的延时时长
            message1.getMessageProperties().setExpiration(ttlTime);
            return message1;
        });
    }
}
